package com.truckdispatching.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.truckdispatching.R;
import com.truckdispatching.UserManager;
import com.truckdispatching.http.controller.UpdatePasswordHttpController;
import com.truckdispatching.http.util.OnResponseHandler;
import com.truckdispatching.util.SharedPreferencesUtil;

/**
 * 修改密码界面
 * 
 * @author Lee
 * @date 2013-4-30
 */
public class ModifyPasswordAcivity extends BaseActivity implements OnClickListener {

	private EditText						mOldPassword;
	private EditText						mNewPassword;
	private EditText						mNewPassword2;

	private Button							mBtnBack;
	private Button							mBtnModify;

	private UpdatePasswordHttpController	mUpdatePasswordHttpController;
	private AlertDialog						mSaveDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify_password_activity);

		mOldPassword = (EditText) findViewById(R.id.old_password);
		mNewPassword = (EditText) findViewById(R.id.new_password);
		mNewPassword2 = (EditText) findViewById(R.id.new_password2);

		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setOnClickListener(this);
		mBtnModify = (Button) findViewById(R.id.btn_modify_ok);
		mBtnModify.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_back: {
				if (checkIsInput()) {
					showSaveDialog();
				} else {
					finish();
				}
				break;
			}
			case R.id.btn_modify_ok:
				if (checkIsInput()) {
					showSaveDialog();
				} else {
					String oldpassword = mOldPassword.getText().toString().trim();
					String password1 = mNewPassword.getText().toString().trim();
					String password2 = mNewPassword2.getText().toString().trim();
					if(oldpassword.equals("")){
						showToast("请输入旧密码");
						mOldPassword.requestFocus();
						break;
					}
					if(password1.equals("")){
						showToast("请输入新密码");
						mNewPassword.requestFocus();
						break;
					}
					if(password2.equals("")){
						showToast("请再次输入密码");
						mNewPassword2.requestFocus();
						break;
					}
					if(!password2.equals(password1)){
						showToast("两次输入密码不相同");
						mNewPassword2.requestFocus();
						break;
					}
					showSaveDialog();
					break;
				}
			default:
				break;
		}
	}

	private boolean checkIsInput() {
		if (!mNewPassword.getText().toString().trim().equals("")) {
			return true;
		}
		if (!mNewPassword2.getText().toString().trim().equals("")) {
			return true;
		}
		return false;
	}

	private void showSaveDialog() {
		if (mSaveDialog == null) {
			mSaveDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("是否保存您的修改？").setPositiveButton("保存", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (mUpdatePasswordHttpController != null) {
						mUpdatePasswordHttpController.cancelHttpRequest();
					}
					mUpdatePasswordHttpController = new UpdatePasswordHttpController();
					final String oldpassword = mOldPassword.getText().toString().trim();
					final String password1 = mNewPassword.getText().toString().trim();
					final String password2 = mNewPassword2.getText().toString().trim();
					if(oldpassword.equals("")){
						showToast("请输入旧密码");
						mOldPassword.requestFocus();
						return;
					}
					if(password1.equals("")){
						showToast("请输入新密码");
						mNewPassword.requestFocus();
						return;
					}
					if(password2.equals("")){
						showToast("请再次输入密码");
						mNewPassword2.requestFocus();
						return;
					}
					if(!password2.equals(password1)){
						showToast("两次输入密码不相同");
						mNewPassword2.requestFocus();
						return;
					}
					
					mUpdatePasswordHttpController.setOnResponseListener(new OnResponseHandler() {

						@Override
						public void onResponseError(int errorCode, String errorMessage) {
							if (errorMessage != null && !errorMessage.equals("")) {
								showToast("修改失败：" + errorMessage);
							} else {
								showToast("修改失败");
							}
							dismissLoadingDialog();
						}

						@Override
						public void onResponse(Object response) {
							showToast("修改成功");
							UserManager.setPassword(password1);
							SharedPreferencesUtil.getInstance().setPassword(password1);
							dismissLoadingDialog();
							finish();
						}
					});
					mUpdatePasswordHttpController.sendRequest(oldpassword, password1);
					showLoadingDialog();
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			}).create();
		}
		mSaveDialog.show();
	}
}
